<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Auth extends CI_Controller {


	function register() {
		
		$this->load->model('auth_model');
		$sprava = 'Zlyhanie registrácie';		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Meno', 'trim|required|xss_clean|min_length[3]');
		$this->form_validation->set_rules('mail', 'mailová adresa', 'trim|required|valid_mail|is_unique[users.mail]');
		$this->form_validation->set_rules('passwd', 'Heslo', '|trim|required|min_length[4]');
		$this->form_validation->set_error_delimiters('<p class = "valid_error">', '</p>');
		
		if ($this->form_validation->run() && $this->auth_model->register()) {			
		
			$odosielatel = 'Letná liga FLL';
			$email = 'noreply@fll.sk'; 
			$predmet = 'Registrácia do systému Letnej ligy FIRST LEGO League';
			$text = 'Tento e-mail Vám bol zaslaný na základe registrácie do systému Letnej ligy FLL'.'<br />'.'<br />'.'Názov tímu: '.$_POST['name'].'<br />'.'Prihlasovací mail: '.$_POST['mail'].'<br />'.'Heslo: '.$_POST['passwd'].'<br />'.'<br />'.'Prihlásiť sa môžete <a href="http://www.fll.sk/fll2013/lltest">tu</a>.'.'<br />'.'<br />'.'Tešíme sa na Vaše riešenia.';
			$prijemca = $_POST['mail']; // email, na ktory sa sprava odosle
			
			if ($odosielatel && $email && $predmet && $text) {
				$hlavicka = "From: " . $odosielatel . " <" . $email . ">\r\n"
				. "Content-Type: text/html; charset=utf-8; format=flowed\n"
				. "MIME-Version: 1.0\n"
				. "Content-Transfer-Encoding: 8bit\n"
				. "Return-Path: <" . $email . "> \n"
				. "X-Mailer: PHP\n";
				mail("$prijemca", "$predmet", "$text", "$hlavicka");
			}
			
			$data['sprava'] = 'Registrácia prebehla úspešne';
			$this->load->model('projekt_model');
			$value = $this->projekt_model->show_table('2013');//date('Y'));
			$data['tabulka'] = $value;
			$this->load->view('index_view',$data);
		
		}
		else {
			$this->load->view('register_view');
		}		

	}

	function testing() {
		echo 'testing funct';
	}	
	
	function login() {
		$testik['chybova'] = '';
		$this->load->model('auth_model');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('mail', 'mailová adresa', 'trim|required');
		$this->form_validation->set_rules('passwd', '|trim|required');
		$this->form_validation->set_error_delimiters('<p class = "valid_error">', '</p>');
		if ($this->form_validation->run()) {
		
			if ($this->auth_model->check()) {
				$data = $this->auth_model->getUserData($_POST['mail']);
				$data['logged_in'] = true;
				$this->session->set_userdata($data); // tuto nastavi tie svoje session array data
				
				redirect('projekt/logged');
			
			}
			else {
				$testik['chybova'] = 'Prihlasovacie meno alebo heslo je nesprávne';
				redirect('projekt');
			}	
			
		}
		else {
			$testik['chybova'] = 'CHYBA';
			redirect('projekt');
		}
		
		
	}

	function logout() {
		
		$this->session->unset_userdata(array('id'=>'', 'name'=>'', 'mail'=>'','logged_in'=>''));
		redirect('projekt');
	
		
	}


	
			

}